package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.service.impl.FrameServiceImpl;
import org.example.util.Result;
import org.example.util.constantUtil.ConstantMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.example.enums.ResultCode.FAIL;
import static org.example.enums.ResultCode.TOKEN_NOT_EXIST;

@RestController
@RequestMapping("frame")
@CrossOrigin
@Slf4j

public class FrameController {
    @Autowired
    private FrameServiceImpl frameService;

    /**
     * 最新数据封装接口
     *
     * @param imei 设备imei号
     * @param bearToken 鉴权token
     * @return 只存在一个ts的原有数据？（待确认）
     */
    @GetMapping("/timeseries")
    public Result getLatestTimeseries(@RequestParam(value = "imei") String imei,
                                      @RequestHeader(value = "Authorization",required = false)String bearToken){
        if(bearToken==null){
            return new Result(TOKEN_NOT_EXIST.code,false,null,"accessToken not exits");
        }
        String token=bearToken.substring(7,bearToken.length());

        return frameService.getLatestTimeseriesWithTitleAndTs(imei,token,null);
    }


    @GetMapping("/timeseries/batch")
    public Result getLatestTimeriesBatch(@RequestParam(value = "pageSize") Integer pageSize,
                                         @RequestParam(value = "page") Integer page,
                                         @RequestParam(value = "textSearch",required = false) String textSearch,  // 某个按name筛选的
                                         @RequestParam(value = "customerTitle",required = false) String customerTitle,  // 项目号，传入则只查该项目号的，否则全查
                                         @RequestHeader(value = "Authorization",required = false)String bearToken){
        if(bearToken==null){
            return new Result(TOKEN_NOT_EXIST.code,false,null,"accessToken not exits");
        }
        String token=bearToken.substring(7,bearToken.length());
        return frameService.getLatestTimeriesBatch(pageSize,page,textSearch,customerTitle,token);
    }


    @GetMapping("/timeseriesHis")
    public Result getTimeseries(@RequestParam(value = "imei") String imei,
                                @RequestParam(value = "keys",required = false) String keys,
                                @RequestParam(value = "startTs",required = false) Long startTs,
                                @RequestParam(value = "endTs",required = false) Long endTs,
                                @RequestHeader(value = "Authorization",required = false)String bearToken){
        if(bearToken==null){
            return new Result(TOKEN_NOT_EXIST.code,false,null,"accessToken not exits");
        }
        String token=bearToken.substring(7,bearToken.length());



        return frameService.getTimeseries(imei,keys,startTs,endTs,token,null);
    }
    @GetMapping("/timeseriesHis/batch")
    public Result getTimeriesBatch(@RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "page") Integer page,
                                   @RequestParam(value = "textSearch",required = false) String textSearch,  // 某个按name筛选的
                                   @RequestParam(value = "customerTitle",required = false) String customerTitle,  // 项目号，传入则只查该项目号的，否则全查
                                   @RequestParam(value = "keys",required = false) String keys,
                                   @RequestParam(value = "startTs",required = false) Long startTs,
                                   @RequestParam(value = "endTs",required = false) Long endTs,
                                   @RequestHeader(value = "Authorization",required = false)String bearToken){
        if(bearToken==null){
            return new Result(TOKEN_NOT_EXIST.code,false,null,"accessToken not exits");
        }
        String token=bearToken.substring(7,bearToken.length());
        return frameService.getTimeseriesBatch(pageSize,page,textSearch,customerTitle,token,keys,startTs,endTs);
    }


    /**
     * 查询存在报警的B1帧数据
     * @param imei
     * @param bearToken
     * @return
     */
    @GetMapping("/alarmData")
    public Result getAlarmData(@RequestParam(value = "imei") String imei,
                               @RequestHeader(value = "Authorization",required = false)String bearToken
                               ){
        if(bearToken==null){
            return new Result(TOKEN_NOT_EXIST.code,false,null,"accessToken not exits");
        }
        String token=bearToken.substring(7,bearToken.length());

        return frameService.getAlarmData(imei,token);
    }

    @GetMapping("/timeseries/alarm")
    public Result getLatestTimeriesByAlarm(@RequestParam(value="error")String error,
                                           @RequestParam(value = "pageSize") Integer pageSize,
                                           @RequestParam(value = "page") Integer page,
                                           @RequestHeader(value = "Authorization",required = false)String bearToken){
        if(bearToken==null){
            return new Result(TOKEN_NOT_EXIST.code,false,null,"accessToken not exits");
        }
        String token=bearToken.substring(7,bearToken.length());
        if(ConstantMapUtil.alarmNameToRequest.get(error)==null){
            return new Result(FAIL.code,false,null,"Alarm Name not exist");
        }
        String defaultValue=ConstantMapUtil.alarmNameToRequest.get(error);

//        return frameService.test(error,token);  // 测试用
        return frameService.getLatestTimeriesByAlarm(defaultValue,token,page,pageSize);

    }


}
